<template>
  <div class="auto">
    <validator name='v'>
      <form class="form-horizontal">
        <div class="row" style="display: flex;justify-content: center;" id='testbill'>
          {{{bill.data}}}
        </div>
        <div class="row" v-if="isEnableBill">
          <div class="col-sm-2" style="margin-left: 10%"  v-if="!messShow">
            <span><strong>发票号:{{model.f_bill_id}}</strong></span>
          </div>
          <div class="col-xs-offset-1" v-if="messShow">
            <span style="color: red"><strong>未获取到收据代码，发票不能正常打印，请确认后补打发票</strong></span>
          </div>
        </div>
        <div class="row" v-if="isEnableBill" style="margin-top: 1%">
          <label for="f_rprint_type" class="col-sm-2 control-label">补打类型:</label>
          <div class="col-sm-2">
            <v-select
              v-model="model.f_rprint_type"
              placeholder='请选择'
              :value.sync="model.f_rprint_type"
              :options='rprinttype'
              close-on-select clear-button>
            </v-select>
          </div>
        </div>
        <div class="row" style="margin-top: 1%">
          <div  :class="[$v.f_cause.required ? 'has-error' : 'has-success']">
            <label for="f_cause" class=" col-sm-2 control-label">补打原因:</label>
            <div class="col-sm-7">
              <textarea class="form-control" v-model="model.f_cause"
                        v-validate:f_cause='{required: true }' rows="3" placeholder="补打原因"></textarea>
            </div>
          </div>
        </div>
      </form>
      <div style="text-align:right;height:auto;margin-top:30px;margin-right:50px;">
        <report-print id="testbill" top='25mm' left='60mm' width='100%' height='80%' style='td,th{font-size: 10pt!important;}'
                      :notrepeat="true" v-if= 'bill.data && model.f_bill_id'></report-print>
        <button class="btn btn-default" @click="clean()">取消</button>
      </div>
    </validator>
  </div>
</template>
<script>
  /**
   *票据补打
   */
  import * as Util from '../../Util'
  import { DataModel } from 'vue-client'
  import AppData from '../../../stores/AppData'
  import co from 'co'

  let getBillGen = function * (self) {

    // 显示票据
    self.bill = new DataModel(self.data.billUrl, {reprint: "'补打'"})
    self.bill.search(self.data.id)

    let data = {
      items: '*',
      condition: `f_user = '${Util.f.name}' and f_bill_state = '启用' and f_bill_type = '${self.data.f_bill_style}'`,
      tablename: 't_billcollec',
      orderitem: 'id'
    }
    console.log('查询票据票号。。', Util.f.name, self.data.f_bill_style)
    let res = yield self.$SqlService.singleTableOrderBy(data)
    // 判断发票管理是否启用
    if (AppData.getSingleValue('是否启用发票管理') === '是') {
      self.isEnableBill = true
      if (res.data.length > 0) {
        if (res.data[0].f_bill_id > res.data[0].f_end_number) {
          res.data[0].f_bill_state = '停用'
          yield self.$resetpost('rs/entity/t_billcollec', res.data[0], {resolveMsg: null, rejectMsg: null})
          self.messShow = true
        } else {
          if (res.data[0].f_bill_id) {
            self.f_billnew_id = res.data[0].f_bill_id
            self.model.f_bill_id = res.data[0].f_bill_id
            self.model.f_billcoll_id = res.data[0].id
          } else {
            self.messShow = true
          }
        }
      } else {
        // 提示没有分配发票号段
        self.$showAlert('收费成功。但发票号段未分配, 请联系相关人员分配发票！稍后可进行票据补打进行打票', 'warning', 5000)
        self.$dispatch('nobillcollec')
      }
    } else {
      self.isEnableBill = false
      self.model.f_bill_id = self.data.id
    }
  }

  export default {
    title: '票据补打',
    data () {
      return {
        bill: Object,
        model: {
          f_bill_id: null,
          f_cause: null,
          f_rprint_type: ['新票号补打']
        },
        isEnableBill: false,
        f_billnew_id: null,
        f_billold_id: null,
        messShow: false,
        rprinttype: [{label: '原票号补打', value: '原票号补打'}, {label: '新票号补打', value: '新票号补打'}]
      }
    },
    props: ['data'],
    watch: {
      'data' (val) {
        this.data = val
      },
      'model.f_rprint_type' (val) {
        if (val[0] === '新票号补打') {
          this.model.f_bill_id = this.f_billnew_id
        } else if (val[0] === '原票号补打') {
          if (!this.f_billold_id) {
            this.model.f_bill_id = this.f_billold_id
          } else {
            this.getOldBillNumber()
          }
        }
        if ((val[0] === '新票号补打' && this.f_billnew_id) || (val[0] === '原票号补打' && this.f_billold_id)) {
          this.messShow = false
        }
      }
    },
    ready () {
      // this.getNewBillNumber()
      this.getBillNumber()
    },
    methods: {
      getBillNumber () {
        co(getBillGen(this))
      },
      getNewBillNumber () {
        this.bill.search(this.data.id)
        let tablename = 't_billcollec'
        let condition = 'f_user = ' + "'" + Util.f.name + "'" + ' and f_bill_state = ' + "'" + '启用' + "'" + ' and f_bill_type = ' + "'" + this.data.f_bill_style + "'"
        this.$SqlService.singleTable(tablename, condition).then((res) => {
          this.f_billnew_id = res.data[0].f_bill_id
          if (res.data[0]. f_bill_id > res.data[0].f_end_number) {
            res.data[0]. f_bill_state = '停用'
            this.$BillService.stopBill(res.data[0])
            this.messShow = true
          } else {
            this.model.f_bill_id = res.data[0].f_bill_id
            this.model.f_billcoll_id = res.data[0].id
            if (!res.data[0].f_bill_id) {
              this.messShow = true
            }
          }
        }).catch((error) => {
          this.messShow = true
        })
      },
      getOldBillNumber () {
        let tablename = 't_billdetail'
        let condition = 'f_billdetail_id = ' + "'" + this.data.id + "'"
        this.$SqlService.singleTable(tablename, condition).then((res) => {
          if (res.data[0].f_bill_id) {
            this.f_billold_id = res.data[0]. f_bill_id
            this.model.f_billcoll_id = res.data[0].f_billcoll_id
            this.model.f_bill_id = res.data[0].f_bill_id
          } else {
            this.messShow = true
          }
        }).catch((error) => {
          this.messShow = true
        })
      },
      clean () {
        this.$info('取消操作')
        this.$dispatch('clean', '票据补打', this.row)
      }
    },
    events: {
      'print' () {
        if (this.model && this.data) {
          let param = {
            detail: {
              f_bill_id: this.model.f_bill_id,
              f_billdetail_id: this.data.id,
              f_bill_state: '已用',
              f_bill_type: `${this.data.type}(补打)`,
              f_operator: Util.f.name,
              f_filiale: Util.f.f_fengongsi,
              f_outlets: Util.f.f_parentname,
              f_cause: this.model.f_cause
            },
            type: '票据补打'
          }
          if (this.isEnableBill){
            param.detail.f_billcoll_id = this.model.f_billcoll_id
            if (this.model.f_rprint_type[0] === '新票号补打') {
              param.billcoll = {
                id: this.model.f_billcoll_id,
                f_bill_id: this.model.f_bill_id + 1
              }
            }
          }
          this.$resetpost('rs/logic/printBill', {data: param}, {resolveMsg: '补打票据成功', rejectMsg: '补打票据失败'}).then((res) => {
            this.$dispatch('success', '票据补打', this.data, res)
          }).catch((error) => {
            this.$dispatch('error', '票据补打', this.data, error)
          })
        }
      },
      'print-error' () {
        this.$showAlert('打印失败，若有需要，请补打发票', 'error', 3)
        this.$dispatch('error', '票据补打', this.data, null)
      }
    }
  }
</script>

<style>
</style>
